package com.inspur.system.service.impl;

import java.util.Date;
import java.util.List;

import com.inspur.common.utils.SecurityUtils;
import com.inspur.common.utils.StringUtils;
import com.inspur.common.utils.uuid.UUID;
import com.inspur.system.domain.vo.SysUserSaveProjVo;
import com.inspur.system.dto.PmDemDTO;
import com.inspur.system.dto.ProNumDTO;
import com.inspur.system.mapper.PmDynMapper;
import com.inspur.system.mapper.SysUserMapper;
import com.inspur.system.utils.PmDynUtils;
import com.inspur.system.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.inspur.system.mapper.PmDemMapper;
import com.inspur.system.domain.PmDem;
import com.inspur.system.service.IPmDemService;

/**
 * 【需求】Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-07
 */
@Service
public class PmDemServiceImpl implements IPmDemService 
{
    @Autowired
    private PmDemMapper pmDemMapper;

    @Autowired
    private PmDynMapper pmDynMapper;

    @Autowired
    private SysUserMapper userMapper;

    /**
     * 查询【需求】
     * 
     * @param cDemNo 【需求】主键
     * @return 【需求】
     */
    @Override
    public PmDemDTO selectPmDemByCDemNo(String cDemNo)
    {
        PmDemDTO pmDemDTO = pmDemMapper.selectPmDemByCDemNo(cDemNo);
        Double cEstWork = pmDemDTO.getcEstWork(); // 预估工时
        Double cRemWork = pmDemDTO.getcRemWork(); // 剩余工时
        String cEstRemWork = cEstWork + "h/" + cRemWork + "h";
        pmDemDTO.setcEstRemWork(cEstRemWork);
        return pmDemDTO;
    }

    /**
     * 查询【需求】列表
     * 
     * @param pmDem 【需求】
     * @return 【需求】
     */
    @Override
    public List<PmDemDTO> selectPmDemList(PmDemDTO pmDem)
    {
        List<PmDemDTO> pmDemDTOList = pmDemMapper.selectPmDemList(pmDem);
        for (PmDemDTO pmDemDTO : pmDemDTOList) {
            Double cEstWork = pmDemDTO.getcEstWork(); // 预估工时
            Double cRemWork = pmDemDTO.getcRemWork(); // 剩余工时
            String cEstRemWork = cEstWork + "h/" + cRemWork + "h";
            pmDemDTO.setcEstRemWork(cEstRemWork);
        }
        return pmDemDTOList;
    }

    /**
     * 导出时查询【需求】列表
     *
     * @param pmDem 【需求】
     * @return 【需求】
     */
    @Override
    public List<PmDem> selectPmDemListExport(PmDem pmDem)
    {
        return pmDemMapper.selectPmDemListExport(pmDem);
    }

    @Override
    public ProNumDTO selectPmDemNum()
    {
        return pmDemMapper.selectPmDemNum();
    }

    /**
     * 新增【需求】
     * 
     * @param pmDem 【需求】
     * @return 结果
     */
    @Override
    public int insertPmDem(PmDem pmDem)
    {
        String pmDemNo = "11" + UUID.generateShortUuid();
        pmDem.setcDemNo(pmDemNo);
        Long userId = SecurityUtils.getUserId();
        pmDem.setcCrePer(userId+"");
        pmDem.setcUpdPer(userId+"");
        pmDem.settCreTm(new Date());
        pmDem.settUpdTm(new Date());
        pmDynMapper.insertPmDyn(PmDynUtils.addDynCom("创建了工作项","1",pmDem.getcDemNo()));
        // 同步用户表
        List<SysUserSaveProjVo> users = UserUtils.updatePmNo(userId,pmDem.getcDemPer(),pmDem.getcDemNo(),pmDem.getcDemHead());
        users.forEach(user -> userMapper.updateUserByProj(user));
        return pmDemMapper.insertPmDem(pmDem);
    }

    /**
     * 修改【需求】
     * 
     * @param pmDem 【需求】
     * @return 结果
     */
    @Override
    public int updatePmDem(PmDem pmDem)
    {
        PmDemDTO oldPmDem = pmDemMapper.selectPmDemByCDemNo(pmDem.getcDemNo());
        if (StringUtils.isNotEmpty(oldPmDem.getcDemState())) {
            if (!oldPmDem.getcDemState().equals(pmDem.getcDemState())) {
                String updateState = PmDynUtils.updateState(oldPmDem.getcDemState(),pmDem.getcDemState());
                pmDynMapper.insertPmDyn(PmDynUtils.addDynCom(updateState,"3",pmDem.getcDemNo()));
            }
        } else {
            if (StringUtils.isNotEmpty(pmDem.getcDemState())) {
                String updateState = PmDynUtils.updateState("0",pmDem.getcDemState());
                pmDynMapper.insertPmDyn(PmDynUtils.addDynCom(updateState,"3",pmDem.getcDemNo()));
            }
        }
        pmDynMapper.insertPmDyn(PmDynUtils.addDynCom("修改了工作项","1",pmDem.getcDemNo()));
        return pmDemMapper.updatePmDem(pmDem);
    }

    /**
     * 批量删除【需求】
     * 
     * @param cDemNos 需要删除的【需求】主键
     * @return 结果
     */
    @Override
    public int deletePmDemByCDemNos(String[] cDemNos)
    {
        return pmDemMapper.deletePmDemByCDemNos(cDemNos);
    }

    /**
     * 删除【需求】信息
     * 
     * @param cDemNo 【需求】主键
     * @return 结果
     */
    @Override
    public int deletePmDemByCDemNo(String cDemNo)
    {
        return pmDemMapper.deletePmDemByCDemNo(cDemNo);
    }
}
